<?php
namespace Home\Controller;

use Common\Controller\HomebaseController;
use Common\Lib\SMSLib;
use Common\Model\MerchantModel;
use Common\Model\MerchantTypeModel;

class IndexController extends HomebaseController
{

    public function _initialize()
    {
        parent::_initialize();
    }

    //首页
    public function Index()
    {

        //banners
        $banners = M('banners')->where(array('use_flg' => 1, 'banner_type' => 'index'))->order('sort ASC')->select();
        $this->assign('banners', $banners);
        //活动
        $activitys = M('activity')->where(array('activity_type' => 1, 'activity_status' => 'Y','activity_end_time'=>array('egt',date('Y-m-d H:i:s'))))->order('sort ASC')->select();
        $this->assign('activitys', $activitys);

        $this->display();
    }

    //活动详情
    public function activity_detail()
    {
        $activityId = I('get.id');
        $activity = M('activity')->where(array('use_flg' => 1, 'id' => $activityId, 'activity_status' => 'Y'))->find();
        $activity_banners = M('activity_banners')->where(array('use_flg' => 1, 'activity_id' => $activityId))->order('sort ASC')->select();

        $this->assign('activity', $activity);
        $this->assign('activity_banners', $activity_banners);
        $this->display();
    }

    //往期回顾
    public function activity_review()
    {
        //活动
        $activitys = M('activity')->where(array('activity_type' => 1, 'activity_status' => 'Y','activity_end_time'=>array('lt',date('Y-m-d H:i:s'))))->order('sort ASC')->select();
        $this->assign('activitys', $activitys);
        $this->display();
    }

    //店铺优惠
    public function shop_discount()
    {
        $banner = M('banners')->where(array('use_flg' => 1, 'banner_type' => 'shop_discount'))->order('sort ASC')->find();

        $activitys = M('activity')->where(array('use_flg' => 1, 'activity_type' => 2, 'activity_status' => 'Y'))->order('sort ASC')->select();

        if ($activitys) {
            foreach ($activitys as &$activity){
               $merchant_array =  explode(',',$activity['merchant_id']);
               if($merchant_array){
                   $activity['merchant'] = M('merchant')->where(array('id' => $merchant_array[0]))->find();

               }
            }
        }

        $this->assign('activitys', $activitys);
        $this->assign('banner', $banner);

        $this->display();
    }

    //会员优惠
    public function member_discount()
    {
        $article = M('article')->where(array('article_type'=>'member_discount'))->find();
        $this->assign('article', $article);
        $this->display();
    }

    //点击查看跳转
    public function homelaundryprice()
    {
        $pre = I('get.par');
        $this->assign('pre', $pre);
        $this->display();
    }

    //商户分享
    public function merchant_share(){
        $merchant_name = I('merchant_name');
        $sort_name = I('sort_name');

        if (!$sort_name) {
            $sort_name = 1;
        }

        $where = array(
            'mc.use_flg' => 1
        );
        $order = array();

        if ($merchant_name) {
            $where['mc.name'] = array('like', "%$merchant_name%");
        }

        if ($sort_name == 1) {
            $order = array('mc.area' => 'ASC');
        } else if ($sort_name == 2) {
            $order = array('mc.name' => 'ASC');
        } else {
            $order = array();
        }

        $merchantModel = new MerchantModel();
        $typeModel = new MerchantTypeModel();
        //$merchants = $merchantModel->where($where)->order($order)->select();
        $merchants = $merchantModel
            ->alias('mc')
            ->field('mc.*,a.id AS merchant_id')
            ->join('JOIN xd_article a ON a.merchant_id = mc.id')
            ->where($where)
            ->order($order)
            ->select();

        foreach ($merchants as &$merchant) {
            $id_array = explode(',', $merchant['type_id']);
            $type_array = array();
            foreach ($id_array as $id) {
                $type = $typeModel->where(array('id' => $id))->find();
                $type_array[] = $type['name'];
            }
            $merchant['type_name_str'] = implode(' / ', $type_array);
        }

        $this->assign('merchant_name', $merchant_name);
        $this->assign('sort_name', $sort_name);

        $this->assign('merchants', $merchants);

        $this->display();
    }

    //page页
    public function page(){
        $id = I('get.id');
        $article =  M('article')->where(array('id'=>$id,'use_flg'=>1))->find();
        $this->assign('article', $article);

        $this->display();
    }
}